﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ForthClassAssignment{
    public partial class MainWindow : Form{

        private TreeNode _preNode;

        private FileInfo _copyFile;

        private TreeNode _choseNode;

        private void Refresh(){
            LoadChild(((DirectoryInfo)(((TreeNode)detail.Tag).Tag)).FullName,detail.Nodes);
            LoadChild(((DirectoryInfo)(((TreeNode)detail.Tag).Tag)).FullName,((TreeNode)detail.Tag).Nodes);
        }
        

        private void Copy_Click(object sender, EventArgs e){
            if (_choseNode != null) {
                _copyFile = ((FileInfo)(_choseNode.Tag));
            }
        }
        private void Paste_Click(object sender, EventArgs e){

            try {
                if (_copyFile != null) {
                    _copyFile.CopyTo(((DirectoryInfo)(detail.Tag)).FullName + "/" + _copyFile.Name);
                    Refresh();
                }
            }
            catch (Exception) {
                status.Text = "粘贴错误,请检查文件是否已经存在!";
            }
        }
        private void Delete_Click(object sender, EventArgs e){
            if (_choseNode != null) {
                (((FileInfo)(_choseNode.Tag))).Delete();
                Refresh();
            }
        }
        private void LoadChild(string path,TreeNodeCollection nodes){
            nodes.Clear();
            var directories = Loader.LoadDirectory(path);
            foreach (var directory in directories) {
                var childNode = new TreeNode(directory.Name);
                childNode.ForeColor = Color.Blue;
                childNode.Tag = directory;
                if(!nodes.Equals(detail.Nodes))
                    childNode.Nodes.Add("");
                nodes.Add(childNode);
            }
            var files = Loader.LoadFile(path);
            foreach (var file in files) {
                var childNode = new TreeNode(file.Name);
                childNode.Tag = file;
                nodes.Add(childNode);
            }
            if (nodes.Count == 0) {
                nodes.Add("空");
            }
            status.Text = path.Replace('\\','>');
        }
        
        private void InitWindow(){
            var disks = DriveInfo.GetDrives();
            foreach (var disk in disks) {
                var childNode = new TreeNode(disk.Name);
                childNode.Tag = new DirectoryInfo(disk.Name);
                childNode.Nodes.Add("");
                treeView.Nodes.Add(childNode);
            }
        }

        private void Expand(object sender, TreeViewEventArgs e){
            var node = e.Node;
            if (node.IsExpanded) {
                var path = ((DirectoryInfo)node.Tag).FullName;
                LoadChild(path, node.Nodes);
            }
        }
        private void Detail(object sender, TreeNodeMouseClickEventArgs e){
            var node = e.Node;
            if (node.Tag is DirectoryInfo) {
                //1
                detail.Tag = node;
                var path = ((DirectoryInfo)node.Tag).FullName;
                LoadChild(path, detail.Nodes);
            }
            else if (node.Tag is FileInfo) {
                FileInfo file = (FileInfo)(node.Tag);
                detail.Nodes.Clear();
                detail.Nodes.Add("名称: " + file.Name);
                detail.Nodes.Add("位置: " + file.DirectoryName);
                string length = file.Length < 1048576
                    ? (file.Length / 1024.0).ToString("N2") + "KB"
                    : (file.Length / 1048576).ToString("N2") + "MB";
                detail.Nodes.Add("大小: " + length );
                detail.Nodes.Add("创建时间: " + file.CreationTime);
                detail.Nodes.Add("修改时间: " + file.LastWriteTime);
            }
        }

        private void Detail_Click(object sender, TreeNodeMouseClickEventArgs e){
            if (e.Node.Tag is FileInfo) {
                _choseNode = e.Node;
            }
        }
        private void Detail_DoubleClick(object sender, TreeNodeMouseClickEventArgs e){
            var node = e.Node;
            var tag = node.Tag;
            if (node.Tag is FileInfo) {
                var file = (FileInfo)tag;
                if (".txt".Equals(file.Extension) || ".exe".Equals(file.Extension)) {
                    try {
                        //使用process进程打开
                        Process.Start(file.FullName);
                    } 
                    catch (Exception) {
                        status.Text = "文件打开失败";
                    }
                }
            }
            else if (node.Tag is DirectoryInfo) {
                detail.Tag = node;
                detail.Nodes.Clear();
                var path = ((DirectoryInfo)node.Tag).FullName;
                LoadChild(path, detail.Nodes);
            }
        }

        private void Backward_Click(object sender, EventArgs e){
            if (detail.Tag == null || ((DirectoryInfo)(((TreeNode)(detail.Tag)).Tag)).Parent == null) {
                status.Text = "没有上一级了";
            }
            else {
                _preNode = ((TreeNode)(detail.Tag));
                var node = (DirectoryInfo)_preNode.Tag;
                var parentNode = node.Parent;
                detail.Tag = _preNode.Parent;
                var path = (parentNode.FullName);
                LoadChild(path, detail.Nodes);
            }
        }

        private void Forward_Click(object sender, EventArgs e){
            if (_preNode != null) {
                detail.Tag = _preNode;
                string path = ((DirectoryInfo)(_preNode.Tag)).FullName;
                LoadChild(path, detail.Nodes);
            }
            else {
                status.Text = "没有下一级了";
            }
        }

        private void AddEvent(){
            treeView.AfterExpand += Expand;
            detail.NodeMouseDoubleClick += Detail_DoubleClick;
            detail.NodeMouseClick += Detail_Click;
            treeView.NodeMouseClick += Detail_Click;
            treeView.NodeMouseClick += Detail;
            backward.Click += Backward_Click; 
            forward.Click += Forward_Click;
            copy.Click += Copy_Click;
            paste.Click += Paste_Click;
            delete.Click += Delete_Click;
            exit.Click += (sender, args) => {
                Close();
            };
        }
        
        public MainWindow(){
            InitializeComponent();
            InitWindow();
            AddEvent();
        }
    }
}